package cn.edu.zjicm.wordsnet_d.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.ViewFlipper;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.ExamRunActivity;
import cn.edu.zjicm.wordsnet_d.data.RelationShip;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;

public class ExamRunMode56Fragment extends Fragment implements OnClickListener{	
	
	public ExamRunMode56Fragment(RelationShip relationShip, int testMode, ExamRunActivity examRunActivity) {
		this.relationShip = relationShip;
		this.testMode = testMode;
		this.mExamRunActivity = examRunActivity;
	}
	
	public ExamRunMode56Fragment(){
		
	}
	
	private ExamRunActivity mExamRunActivity;
	// Data
	private RelationShip relationShip;
	private int testMode;

	// View
	private TextView mRelationShipText1, mRelationShipText2;
	private LinearLayout mWordsListContainer;
	//
	private TextView displayButton;
	private Button rememberButton, forgetButton;
	private ViewFlipper buttonFlipper; 
	private FrameLayout displayFlipper; 
	
	private ScrollView wordDisplayView;
	private int id;
	
	private WordFactory wf;
	private LayoutInflater mInflater;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// return null;
		View v = inflater.inflate(R.layout.fragment_exam_run_mode56, null);
		mRelationShipText1 = (TextView) v.findViewById(R.id.relationship_text1);
		mRelationShipText2 = (TextView) v.findViewById(R.id.relationship_text2);
		mWordsListContainer = (LinearLayout) v.findViewById(R.id.word_list_item_container);

		displayButton = (TextView) v.findViewById(R.id.test_display_button);
		rememberButton = (Button) v.findViewById(R.id.test_remember_button);
		forgetButton = (Button) v.findViewById(R.id.test_forget_button);
		displayFlipper = (FrameLayout) v.findViewById(R.id.diplay_flipper); 
		buttonFlipper = (ViewFlipper) v.findViewById(R.id.button_flipper); 
		wordDisplayView = (ScrollView) v.findViewById(R.id.word_detail_scroll_view); 
		
		buttonFlipper.setDisplayedChild(1);
		
		wf = WordFactory.getInstance(getActivity());
		mInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		if(relationShip != null)
			initView();
		return v;
	}	
	
	public void initViewForNewRelationShip(RelationShip relationShip, int testMode){
		this.relationShip = relationShip;
		this.testMode = testMode;
		initView();
	}

	private void initView() {
		if(testMode == 5) {
			mRelationShipText1.setText(relationShip.getColloc_eng());   
//			mRelationShipText2.setText(relationShip.getColloc_cn());
		} else if(testMode == 6) {
			mRelationShipText1.setText(relationShip.getColloc_cn());
//			mRelationShipText2.setText(relationShip.getColloc_eng());			
		}
		displayButton.setVisibility(View.VISIBLE);
	
		mRelationShipText2.setText(" ");
		CreatView.reLoadWordListView(mWordsListContainer, mInflater, relationShip, wf, getActivity());
		
		//displayFlipper.setDisplayedChild(0);
//		displayButton.setVisibility(View.VISIBLE);
		wordDisplayView.setVisibility(View.GONE);
		
		displayFlipper.setOnClickListener(this);
		rememberButton.setOnClickListener(this);
		forgetButton.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if(view ==displayFlipper) {
			//displayFlipper.setDisplayedChild(1);
			displayButton.setVisibility(View.GONE);
			if(testMode == 5) {
				mRelationShipText2.setText(relationShip.getColloc_cn());
			} else if(testMode == 6) {
				mRelationShipText2.setText(relationShip.getColloc_eng());			
			}
			
			wordDisplayView.setVisibility(View.VISIBLE);
		} else if(view == forgetButton) {
			mExamRunActivity.next(false);			
		} else if(view == rememberButton) {
			mExamRunActivity.next(true);			
		}
	}
}